<?php

namespace app\controller\admin;

use app\BaseController;
use app\repositories\DefaultModel\admin\AdminRepository;
use jwt\Jwt;
use think\facade\Db;

class RouterController extends BaseController
{
    public function routerList()
    {

        $tokenRst = app('auth')->user();
        $where = [];

        $role = Db::name("admin")->where("id", $tokenRst['id'])->value("role");

        $treeList = Db::name("role")->where("id", $role)->value("treeList");

        $treeStr = implode(',', array_map('change_to_quotes', json_decode($treeList, true)));


//        if ($tokenRst['username'] != 'admin') {
//            $where = " path in ($treeStr)";
//        }
        $result = Db::name("menu")->where("parent_id", 0)->where($where)->order("sorts desc")->select();
        $returnRst = [];
        foreach ($result as $row) {
            $tmp = getTree($row);
            $children = Db::name("menu")->where("parent_id", $row['id'])->where($where)->order("sorts desc")->select();
            if ($children) {
                foreach ($children as $crow) {
//                    if (in_array($crow['path'], $ts)) {
                    $tmp['children'][] = getTree($crow);
//                    }
                }
            }
//
            $returnRst[] = $tmp;
        }
        return retJson(['list' => $returnRst]);

    }
}
